//
//  main.m
//  HomeWork 07
//
//  Created by dllo on 16/7/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Province.h"
#import "City.h"
#import "Zone.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
    NSString *datastring = [NSString stringWithContentsOfFile:@"/Users/dllo/Downloads/Homework-05/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *arrOfPlace = [datastring componentsSeparatedByString:@"\n"];
    NSMutableArray *allProArray = [NSMutableArray array];
    for (NSString *area in arrOfPlace) {
        if (![area hasPrefix:@" "]) {
            Province *pro = [Province zoneWithName:area];
            [allProArray addObject:pro];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *pro = [allProArray lastObject];
            City *city = [City zoneWithName:area];
            [pro.marr addObject:city];
        } else {
            
            City *city = [[[allProArray lastObject] marr] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.marr addObject:zone];
        }
    }
    NSLog(@"%@",allProArray);
    

    NSString *source = [NSString stringWithContentsOfFile:@"/Users/dllo/Downloads/Homework-05/Location1.txt" encoding:NSUTF8StringEncoding error:nil];
   
    NSArray *allCityArray = [source componentsSeparatedByString:@","];
       NSMutableArray *allTempCityArray = [NSMutableArray array];
    for (NSString *cityString in allCityArray) {
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        TempCity *temcity =  [[TempCity alloc]initWithName:[city firstObject] cityNumber:[city lastObject]];
        [allTempCityArray addObject:temcity];
    }

    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc]initWithKey:@"cityNumber" ascending:YES];
    [allTempCityArray sortUsingDescriptors:@[sortTempCity]];
    
    NSMutableArray *finalClassArr = [NSMutableArray array];
    for (TempCity *tempcity in allTempCityArray) {
        if ([tempcity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [Province zoneWtihTempCity:tempcity];
            [finalClassArr addObject:pro];
        } else if ([tempcity.cityNumber hasSuffix:@"00"] && [tempcity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [finalClassArr lastObject];
            City *city = [City zoneWtihTempCity:tempcity];
            [pro.marr addObject:city];
        }else {
            Province *pro = [finalClassArr lastObject];
            City *city = [pro.marr lastObject];
            Zone *zone = [Zone zoneWtihTempCity:tempcity];
            [city.marr addObject:zone];
        }
    }
    
    NSLog(@"222222:%@",finalClassArr);

    
    
    
    
    
    return 0;
}
